package com.splic.web.controller.product;

import com.splic.common.annotation.Log;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.AjaxResult;
import com.splic.common.core.domain.ResponseObj;
import com.splic.common.core.page.TableDataInfo;
import com.splic.common.enums.BusinessType;
import com.splic.common.exception.BusinessException;
import com.splic.common.utils.StringUtils;
import com.splic.common.utils.poi.ExcelUtil;
import com.splic.product.domain.Product;
import com.splic.product.domain.Work;
import com.splic.product.service.ProductService;
import com.splic.product.service.WorkService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 事件同步Controller
 * 
 * @author singe
 * @date 2023-06-18
 */
@Controller
@RequestMapping("/product/work")
@Slf4j
public class WorkController extends BaseController
{
    private String prefix = "product/work";

    @Autowired
    private WorkService workServiceImpl;
    @Autowired
    private ProductService productServiceImpl;

    /**
     * 查询同步
     *
     * @return
     */
    @GetMapping("/judgeSync")
    public ResponseObj<Work> judgeSync(Work workReq) {
        try{
            Work work = workServiceImpl.judgeSync(workReq);
            return ResponseObj.SUCCESS(work);
        } catch (BusinessException e){
            if (e.getCode() != -100) {
                return ResponseObj.ERROR(e.getMessage());
            } else {
                return ResponseObj.ERROR(e.getMessage(), String.valueOf(e.getCode()));
            }
        } catch (Exception e){
            log.error("同步判断失败:{}",e);
            return ResponseObj.ERROR("同步判断失败。");
        }
    }

    /**
     * 同步结束
     *
     * @return
     */
    @GetMapping("/finishSync")
    public ResponseObj<Integer> finishSync(Work workReq) {
        try{
            return ResponseObj.SUCCESS(workServiceImpl.finishSync(workReq));
        } catch (BusinessException e){
            return ResponseObj.ERROR(e.getMessage(), String.valueOf(e.getCode()));
        } catch (Exception e){
            log.error("结束同步失败:{}",e);
            return ResponseObj.ERROR("结束同步失败。");
        }
    }

    @RequiresPermissions("product:work:view")
    @GetMapping()
    public String work(ModelMap mmap)
    {
        List<Product> productList = productServiceImpl.selectProductList(null);
        mmap.put("productList",productList);
        return prefix + "/list";
    }

    /**
     * 查询事件同步列表
     */
    @RequiresPermissions("product:work:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Work work)
    {
        startPage();
        List<Work> list = workServiceImpl.selectWorkList(work);
        return getDataTable(list);
    }

    /**
     * 新增事件同步
     */
    @GetMapping(value = { "/form/{id}", "/form" })
    public String form(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id)){
            mmap.put("work", workServiceImpl.selectWorkById(id));
        }else{
            mmap.put("work", new Work());
        }
        return prefix + "/form";
    }


    /**
     * 新增保存事件同步
     */
    @RequiresPermissions("product:work:edit")
    @Log(title = "事件同步", businessType = BusinessType.INSERT)
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(Work work)
    {
        return toAjax(workServiceImpl.saveOrUpdate(work));
    }

    /**
     * 删除
     */
    @RequiresPermissions("product:work:remove")
    @Log(title = "事件同步", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long id)
    {
        return toAjax(workServiceImpl.deleteById(id));
    }


    /**
     * 导出事件同步列表
     */
    @RequiresPermissions("product:work:export")
    @Log(title = "事件同步", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Work work)
    {
        List<Work> list = workServiceImpl.selectWorkList(work);
        ExcelUtil<Work> util = new ExcelUtil<Work>(Work.class);
        return util.exportExcel(list, "事件同步数据");
    }
}
